package com.dongwu.security.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.dongwu.common.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

@Entity
@Table(name = "sys_group")
public class Group extends BaseEntity<String>{
    
    // 角色名称
	@Column(unique=true)
    private String name;
    
    private String remark;

    @JsonIgnore
    @ManyToMany(cascade = {}, fetch = FetchType.LAZY)
    @JoinTable(name = "sys_group_role",
            joinColumns = {@JoinColumn(name = "group_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id")})
    @Fetch(value = FetchMode.SUBSELECT)
    private List<Role> roles = Lists.newArrayList();

    @Deprecated
    @JsonIgnore
    @ManyToMany(cascade = {}, fetch = FetchType.LAZY)
    @JoinTable(name = "sys_group_user",
            joinColumns = {@JoinColumn(name = "group_id")},
            inverseJoinColumns = {@JoinColumn(name = "user_id")})
    @Fetch(value = FetchMode.SUBSELECT)
    private List<User> users = Lists.newArrayList();
    
    public Group() {
 
	}

	public Group(String name) {
		this.name = name;
	}
    
    public Group(String name, String remark) {
		this.name = name;
		this.remark = remark;
	}

	public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public String getRemark() {
		return remark;
	}

	public void setRemark(String desc) {
		this.remark = desc;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Group [name=" + name + ", remark=" + roles + "]";
	}

	@Transient
	public String getUserNames() {
		List<String> names = Lists.newArrayList();
		for (User user : users) {
			names.add(user.getName());
		}
		return Joiner.on(",").join(names);
	}

	@Transient
	public String getUserIds() {
		List<String> ids = Lists.newArrayList();
		for (User user : users) {
			ids.add(user.getId());
		}
		return Joiner.on(",").join(ids);
	}

	@Transient
	public String getRoleNames() {
		List<String> names = Lists.newArrayList();
		for (Role role : roles) {
			names.add(role.getRemark());
		}
		return Joiner.on(",").join(names);
	}

	@Transient
	public String getRoleIds() {
		List<String> ids = Lists.newArrayList();
		for (Role role : roles) {
			ids.add(role.getId());
		}
		return Joiner.on(",").join(ids);
	}


}
